﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Extensions
{
	using System.Web.Mvc;
	using System.Web.Routing;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;

	public static class UserBadgeExtension
	{
		/// <summary>
		/// Gets the user badge icon image.
		/// </summary>
		/// <param name="badge">The badge.</param>
		/// <returns></returns>
		public static string IconImage(this UserBadge badge)
		{
			return IconImage(badge, null);
		}

		/// <summary>
		/// Gets the user badge icon image.
		/// </summary>
		/// <param name="badge">The badge.</param>
		/// <param name="attributes">The attributes.</param>
		/// <returns></returns>
		public static string IconImage(this UserBadge badge, object attributes)
		{
			return IconImage(badge.FilePath, attributes);
		}

		/// <summary>
		/// Gets the user badge icon image.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static string IconImage(this UserBadgeUpdate value)
		{
			return IconImage(value, null);
		}

		/// <summary>
		/// Gets the user badge icon image.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="attributes">The attributes.</param>
		/// <returns></returns>
		public static string IconImage(this UserBadgeUpdate value, object attributes)
		{
			return IconImage(value.FilePath, attributes);
		}

		private static string IconImage(string path, object attributes)
		{
			var builder = new TagBuilder("img");

			builder.MergeAttribute("src", path.AsRoot());
			builder.MergeAttribute("alt", Resources.Titles.Avatar);
			builder.MergeAttributes(new RouteValueDictionary(attributes));

			return builder.ToString(TagRenderMode.SelfClosing);
		}
	}
}